package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.*;
import cn.itcast.domain.cargo.*;
import cn.itcast.domain.vo.ExportProductResult;
import cn.itcast.domain.vo.ExportResult;
import cn.itcast.service.cargo.ExportService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.dubbo.config.annotation.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

@Service
public class ExportServiceImpl implements ExportService {

    @Autowired
    private ExportDao exportDao;
    @Autowired
    private ContractProductDao contractProductDao;
    @Autowired
    private ExportProductDao exportProductDao;
    @Autowired
    private ContractDao contractDao;
    @Autowired
    private ExtEproductDao extEproductDao;
    @Override
    public Export findById(String id) {
        return exportDao.selectByPrimaryKey(id);
    }

    @Override
    public void save(Export export) {
        String contractIds = export.getContractIds();
        List<String> contractIdList = Arrays.asList(contractIds.split(","));
        ContractProductExample contractProductExample = new ContractProductExample();
        contractProductExample.createCriteria().andContractIdIn(contractIdList);
        List<ContractProduct> contractProductsList = contractProductDao.selectByExample(contractProductExample);
        for (ContractProduct contractProduct : contractProductsList) {
            ExportProduct exportProduct = new ExportProduct();
            BeanUtils.copyProperties(contractProduct,exportProduct);
            exportProduct.setExportId(export.getId());
            exportProductDao.insertSelective(exportProduct);
            List<ExtCproduct> extCproducts = contractProduct.getExtCproducts();
            for (ExtCproduct extCproduct : extCproducts) {
                ExtEproduct extEproduct = new ExtEproduct();
                BeanUtils.copyProperties(extCproduct,extEproduct);
                extEproduct.setExportId(export.getId());
                extEproduct.setExportProductId(extEproduct.getId());
                extEproductDao.insertSelective(extEproduct);
            }
        }
        ContractExample contractExample = new ContractExample();
        contractExample.createCriteria().andIdIn(contractIdList);
        List<Contract> contracts = contractDao.selectByExample(contractExample);
        int proNum=0;
        int extNum=0;
        StringBuilder sb = new StringBuilder();
        for (Contract contract : contracts) {
            proNum+=contract.getProNum();
            extNum+=contract.getExtNum();
            sb.append(contract.getContractNo()).append(" ");
        }
        export.setProNum(proNum);
        export.setExtNum(extNum);
        export.setCustomerContract(sb.toString());
        exportDao.insertSelective(export);
    }

    @Override
    public void update(Export export) {
        List<ExportProduct> exportProducts = export.getExportProducts();
        if(exportProducts!=null&&exportProducts.size()>0){
            for (ExportProduct exportProduct : exportProducts) {
                exportProductDao.updateByPrimaryKeySelective(exportProduct);
            }
        }
        exportDao.updateByPrimaryKeySelective(export);
    }

    @Override
    public void delete(String id) {
        //报运单号
        Export export = exportDao.selectByPrimaryKey(id);
        //export所有的货物
        ExtEproductExample extEproductExample = new ExtEproductExample();
        extEproductExample.createCriteria().andExportIdEqualTo(id);
        List<ExtEproduct> list = extEproductDao.selectByExample(extEproductExample);
        for (ExtEproduct extEproduct : list) {
            extEproductDao.deleteByPrimaryKey(extEproduct.getId());
        }
        ExportProductExample exportProductExample = new ExportProductExample();
        exportProductExample.createCriteria().andExportIdEqualTo(id);
        List<ExportProduct> exportProductsList = exportProductDao.selectByExample(exportProductExample);
        for (ExportProduct exportProduct : exportProductsList) {
            exportProductDao.deleteByPrimaryKey(exportProduct.getId());
        }
        exportDao.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo findAll(ExportExample example, int page, int size) {
        PageHelper.startPage(page,size);
        List<Export> list = exportDao.selectByExample(example);
        return new PageInfo(list);
    }

    @Override
    public void updateE(ExportResult exportResult) {
        Export export = new Export();
        export.setId(exportResult.getExportId());
        export.setState(exportResult.getState());
        export.setRemark(exportResult.getRemark());
        exportDao.updateByPrimaryKeySelective(export);
        Set<ExportProductResult> products = exportResult.getProducts();
        for (ExportProductResult product : products) {
            ExportProduct exportProduct = new ExportProduct();
            exportProduct.setId(product.getExportProductId());
            exportProduct.setTax(product.getTax());
            exportProductDao.updateByPrimaryKeySelective(exportProduct);
        }
    }
}
